home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.1 / Libraries / Intuition / other_examples / PubSc / button.c next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  4.2 KB  |  196 lines

  1. /* button.c -- labeled state buttons :ts=4    */
  2.  
  3. /*
  4. Copyright (c) 1989 Commodore-Amiga, Inc.
  5.  
  6. Executables based on this information may be used in software
  7. for Commodore Amiga computers. All other rights reserved.
  8. This information is provided "as is"; no warranties are made.
  9. All use is at your own risk, and no liability or responsibility
  10. is assumed.
  11. */
  12.  
  13. #include <sysall.h>
  14.  
  15. #define D(x)    ;
  16. #define DT(x)    ;
  17.  
  18. /* uses two images, On and Off    */
  19. static struct Image *bOffImage = NULL;        /* shared Image */
  20. static struct Image *bOnImage = NULL;        /* shared Image */
  21. /* text offset beyond box portion of image */
  22. static int            bTextLeft;
  23. static int            bTextTop;
  24.  
  25. struct Image    *CreateImage();
  26. struct Image    *makeButtonImages();
  27. struct Gadget    *CreateBoolGadget();
  28. struct RastPort    *CreateImageRPort();
  29.  
  30. /*
  31.  *    creates linked list of "button" state gadgets, with
  32.  *    sequential GadgetID values.
  33.  */
  34. struct Gadget    *
  35. buttonGadgets( rp, argv, numgad, id )
  36. struct RastPort *rp;
  37. char    **argv;
  38. {
  39.     int                    num;
  40.     char                **a;
  41.     struct Rectangle    textent;
  42.     struct Gadget        *glist = NULL;
  43.     struct Gadget        *g;
  44.  
  45.     g = (struct Gadget *) &glist;
  46.  
  47.     textListExtent( rp, argv, numgad, &textent );
  48.  
  49.     if ( makeButtonImages( rp, &textent ) )
  50.     {
  51.         /* make gadget for each line of text */
  52.         for ( a = argv, num = numgad; num; ++a, --num )
  53.         {
  54.             if (g->NextGadget=CreateBoolGadget(bOffImage,NULL,id++,*a))
  55.             {
  56.                 g = g->NextGadget;
  57.                 D( printf("bG: gadget %lx\n", g ) );
  58.  
  59.                 /* position gadget text */
  60.                 g->GadgetText->LeftEdge = bTextLeft;
  61.                 g->GadgetText->TopEdge = bTextTop;
  62.  
  63.                 /* set gadget flags */
  64.                 g->Activation |= (GADGIMMEDIATE | TOGGLESELECT);
  65.                 g->Flags = (g->Flags & ~GADGHIGHBITS) | GADGHIMAGE;
  66.                 g->SelectRender = (APTR) bOnImage;
  67.             }
  68.             else
  69.             {
  70.                 FreeGadgets( glist );
  71.                 glist = NULL;
  72.                 freeButtonImages();
  73.                 break;
  74.             }
  75.         }
  76.     }
  77.     D( printf("bG return: %lx\n", glist ) );
  78.     return ( glist );
  79. }
  80.  
  81. cleanupButtonGadgets()
  82. {
  83.     freeButtonImages();
  84. }
  85.  
  86. /* feel free to localize these suckers    */
  87. UBYTE    *onoff[] = { (UBYTE *) "Off", (UBYTE *) "On" };
  88.  
  89. /*
  90.  * creates two images, returns pointer to first, NULL if failure.
  91.  */
  92. struct Image    *
  93. makeButtonImages( textrp, extent )
  94. struct RastPort        *textrp;            /* for size of On/Off strings */
  95. struct Rectangle    *extent;
  96. {
  97.     struct RastPort    *rp;
  98.     struct RastPort    clonerp;
  99.  
  100.     int    width, height;
  101.     int    oowidth, ooheight;
  102.     struct Rectangle    ooextent;
  103.  
  104.     /* stuff for two passes at image creation    */
  105.     int        i;
  106.     UBYTE    *imtext;
  107.     struct Image    **doimage;
  108.     UBYTE    fieldpen, textpen;
  109.  
  110.  
  111.     /* get size of on/off box    */
  112.     textListExtent( textrp, onoff, 2, &ooextent );
  113.     oowidth = ooextent.MaxX - ooextent.MinX + 1;
  114.     ooheight = ooextent.MaxY - ooextent.MinY + 1;
  115.     DT( printf("oowidth: %d, ooheight %d\n", oowidth, ooheight ));
  116.     oowidth        += 10;            /* scale by resolution soon    */
  117.     ooheight    += 6;
  118.  
  119.     /* set up intuitext origins    */
  120.     bTextLeft    = oowidth + 16;
  121.     bTextTop    = (ooheight - textrp->TxHeight)/2;
  122.  
  123.     /* set up IntuiText positions to the right of the on/off box    */
  124.  
  125.     width = extent->MaxX - extent->MinX + 1 + bTextLeft;
  126.     height = ooheight;                    /* using same font/metric    */
  127.     width    += 6;                        /* scale by resolution soon    */
  128.  
  129.     if ( !bOffImage )
  130.     {
  131.         for ( i = 0; i < 2; ++i )
  132.         {
  133.             imtext = onoff[ i ];
  134.             if ( i )
  135.             {
  136.                 doimage = &bOnImage;
  137.                 fieldpen = 2;
  138.                 textpen = 1;
  139.             }
  140.             else
  141.             {
  142.                 fieldpen = 1;
  143.                 textpen = 2;
  144.                 doimage = &bOffImage;
  145.             }
  146.  
  147.             if ( *doimage = CreateImage( width, height, 2 ) )
  148.             {
  149.                 if ( rp = CreateImageRPort( *doimage ) )
  150.                 {
  151.                     drawBox( rp, oowidth, ooheight, fieldpen, 3, 2, 1 );
  152.  
  153.                     /* clone font from textrp    */
  154.                     clonerp = *textrp;
  155.                     clonerp.Layer = NULL;
  156.                     clonerp.BitMap = rp->BitMap;
  157.  
  158.                     SetDrMd( &clonerp, (long) JAM1 );
  159.                     SetAPen( &clonerp, (long) textpen );
  160.  
  161.                     Move( &clonerp,
  162.                     (long) (oowidth-textLength( &clonerp, imtext ))/2,
  163.                     (long) (ooheight - clonerp.TxHeight)/2 +
  164.                         clonerp.TxBaseline);
  165.  
  166.                     Text( &clonerp, imtext, (long) strlen( imtext ) );
  167.  
  168.                     DeleteImageRPort( rp );
  169.                 }
  170.                 else
  171.                 {
  172.                     freeButtonImages();        /* nulls out bOffImage */
  173.                     break;
  174.                 }
  175.             }
  176.         }
  177.     }
  178.  
  179.     D( printf("mBI returning %lx\n", bOffImage ) );
  180.     return ( bOffImage );
  181. }
  182.  
  183. freeButtonImages()
  184. {
  185.     if ( bOffImage ) DeleteImage( bOffImage );
  186.     bOffImage = NULL;
  187.  
  188. #if 0
  189.     if ( bOnImage ) DeleteImage( bOnImage );
  190.     bOnImage = NULL;
  191. #endif
  192. }
  193.  
  194.  
  195.  
  196.